package com.struts.action.product;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import java.util.List;
import com.spring.service.product.ProductAndAtrributeService;
import com.struts.form.ProductAndProductAtrributeForm;

public class ProductAndAtrributeAction extends DispatchAction{
	
	private ProductAndAtrributeService productAndAtrributeService=null;
	
	/**
	 * save productid and atrributeid
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward saveProductAndAtrribute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		List list=new ArrayList();
		ProductAndProductAtrributeForm papa=new ProductAndProductAtrributeForm();
		
		String atrribute_id=request.getParameter("atrribute_id");
		String atrribute_id_array[]=atrribute_id.split(",");
		
		papa.setProduct_id(Long.parseLong(request.getParameter("product_id")));
		papa.setAtrribute_id(Long.parseLong(atrribute_id_array[0]));
		papa.setAtrribute_value(request.getParameter("atrribute_value"));
		list.add(papa);
		papa=new ProductAndProductAtrributeForm();
		papa.setProduct_id(Long.parseLong(request.getParameter("product_id")));
		papa.setAtrribute_id(Long.parseLong(atrribute_id_array[1]));
		papa.setAtrribute_value(request.getParameter("atrribute_value1"));
		list.add(papa);
		
		if(productAndAtrributeService.saveProductAndAtrribute(list))
		{
			String path = mapping.findForward("success").getPath();
			ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&productleibieID="+request.getParameter("productleibieID"));
			forward.setRedirect(true);
			return forward;
		}
		else
			return mapping.findForward("fail");
	}
	
	
	
	public ActionForward updateProductAndAtrribute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		List list=new ArrayList();
		ProductAndProductAtrributeForm papa=new ProductAndProductAtrributeForm();
		
		String atrribute_id=request.getParameter("atrribute_id");
		String atrribute_id_array[]=atrribute_id.split(",");
		
		papa.setId(Long.parseLong(atrribute_id_array[0]));
		papa.setProduct_id(Long.parseLong(request.getParameter("product_id")));
		papa.setAtrribute_id(1L);
		papa.setAtrribute_value(request.getParameter("atrribute_value"));
		list.add(papa);
		papa=new ProductAndProductAtrributeForm();
		papa.setProduct_id(Long.parseLong(request.getParameter("product_id")));
		papa.setAtrribute_id(2L);
		papa.setId(Long.parseLong(atrribute_id_array[1]));
		papa.setAtrribute_value(request.getParameter("atrribute_value1"));
		list.add(papa);
		
		if(productAndAtrributeService.updateProductAndAtrribute(list))
		{
			String path = mapping.findForward("success").getPath();
			ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&productleibieID="+request.getParameter("productleibieID"));
			forward.setRedirect(true);
			return forward;
		}
		else
			return mapping.findForward("fail");
	}

	
	
	public ProductAndAtrributeService getProductAndAtrributeService() {
		return productAndAtrributeService;
	}

	public void setProductAndAtrributeService(
			ProductAndAtrributeService productAndAtrributeService) {
		this.productAndAtrributeService = productAndAtrributeService;
	}

}
